limsBatch.detailPage = {

    getBtnToolbar : function() {
        var _this = this;
        if (_this.param.readonly === '1'){
            return {
                type : 'btnToolbar',
                items : [{
                    type : 'button',
                    text : 'GIKAM.BUTTON.BACK',
                    icon : 'back',
                    onClick : function() {
                        if (Gikam.isEmpty(_this.param.topPage)){
                            workspace.window.goBack();
                        }else{
                            Gikam.getLastModal().close();
                        }
                    }
                }]
            }
        }
        return {
            type : 'btnToolbar',
            items : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.BACK',
                icon : 'back',
                onClick : function() {
                    workspace.window.goBack();
                }
            }, {
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                icon: 'submit', color: 'success',
                onClick: function () {
                    var data = Gikam.getComp('batch-sample-list-grid').getData();
                    if (Gikam.isEmpty(data)) {
                        Gikam.alert('BATCH.SUBBMIT.CHECK_SAMPLE_NOTEMPTY');
                        return;
                    }
                    var submitUrl = Gikam.printf(limsBatch.baseUrl + '/submitById/{id}', {
                        id: _this.param.id
                    })
                    Gikam.confirm('GIKAM.MODAL.SUBMIT', 'GIKAM.TIP.SUBMIT', function () {
                        workspace.window.showMask()
                        Gikam.postText(submitUrl, Gikam.getJsonWrapper(null, [null, null])).done(function () {
                            workspace.window.goBack();
                        }).always(function () {
                            workspace.window.closeMask()
                        })
                    })
                }
            } ]
        }
    },

    getBaseInfoTab : function() {
        var _this = this;
        return {
            type : 'tab',
            panels : [ {
                title : 'MODULE.BUSINESS.ORDERTASK.TAB.SAMPLE_INFO',
                items : [ {
                    type : 'grid',
                    id : 'batch-sample-list-grid',
                    url : limsBatch.batchSampleUrl + '/select/samples',
                    requestData : {
                        batchId_EQ : Gikam.isEmpty(_this.param.id) ? '-1' : _this.param.id
                    },
                    columns : limsBatch.getSampleListColumns(),
                    onCellClick : function(field, row) {
                        field === 'sampleCode' && _this.editSampleDetail(row);
                    },
                    deleteFormatter: function (row) {
                        return row.sampleCode || row.id
                    },
                    onRendered: function () {
                        if(_this.param.readonly === '1') {
                            this.hideToolbar();
                        }
                    },
                    toolbar : _this.getSampleToolbar()
                } ]
            }, {
                title : 'MODULE.BUSINESS.ORDERTASK.TAB.SAMPLE_LOGISTICS_INFO',
                items : [ {
                    type : 'grid',
                    url : limsBatch.batchSampleUrl + '/select/samples',
                    requestData : {
                        batchId_EQ : Gikam.isEmpty(_this.param.id) ? '-1' : _this.param.id
                    },
                    service : 'limsServiceImpl',
                    id : 'logistics-info-grid',
                    columns : limsBatch.getLogisticsListColumns(),
                    onCellClick : function(field, row) {
                        field === 'sampleCode' && _this.editLogisticsDetail(row);
                    },
                    onRendered: function () {
                        if(_this.param.selected === '1' || _this.param.readonly === '1') {
                            this.hideToolbar();
                        }
                    },
                    toolbar : [ {
                        type : 'button',
                        text : 'GIKAM.BUTTON.EDIT',
                        icon : 'edit',
                        onClick : function() {
                            let selections = Gikam.getComp('logistics-info-grid').getSelections();
                            if (Gikam.isEmpty(selections)) {
                                Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                                return;
                            }
                            Gikam.createLimsModal({
                                title: 'GIKAM.BUTTON.EDIT',
                                width : 600,
                                height : 300,
                                onConfirm: function () {
                                    var form = Gikam.getComp('sample-logistics-form');
                                    if (form.validate()) {
                                        var data = form.getData();
                                        var chooseData = selections.map(e => {
                                            return {
                                                id : e.id,
                                                temperatureLimits : data.temperatureLimits,
                                                senderName : data.senderName,
                                                senderPhone : data.senderPhone,
                                                courierNumber : data.courierNumber,
                                                courier : data.courier,
                                                senderRemark : data.senderRemark,
                                            }
                                        })
                                        Gikam.put(limsBatch.sampleUrl + '/instant', Gikam.getJsonWrapper(null, [null, chooseData])).done(function () {
                                            Gikam.getComp('logistics-info-grid').refresh()
                                        })
                                        Gikam.getLastModal().close(data);
                                    }
                                },
                                m: {
                                    type: 'form',
                                    id: 'sample-logistics-form',
                                    columns : 2,
                                    titleWidth : 100,
                                    fields: limsBatch.getSampleLogisticsFormFields(),
                                },
                            })
                        }
                    }]
                } ]
            }, {
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [{
                    type: 'uploader',
                    id: 'lims-batch-special-uploader',
                    dbTable: 'T_LIMS_BATCH',
                    bizId: Gikam.isEmpty(_this.param.id) ? '-1' : _this.param.id,
                    readonly : _this.param.readonly === '1'
                }]
            }]
        }
    },

    editSampleDetail : function (row) {
        var _this = this;
        var model = Gikam.create('modal', {
            title: 'GIKAM.BUTTON.EDIT',
            width : 600,
            height : 450,
        });
        Gikam.create('layout', {
            renderTo: model.window.$dom,
            center: {
                items: [{
                    type: 'form',
                    id: 'batch-sample-code-form',
                    columns : 2,
                    titleWidth : 110,
                    fields: limsBatch.getSampleFormFields(),
                    autoSave: false,
                    onRendered: function () {
                        this.hideFields(['sampleCode']);
                        row.productNames = row.ext$.productnames;
                        this.setData(row)
                        if(_this.param.readonly === '1') {
                            this.setReadonly(true);
                        }
                        if(_this.param.selected === '1') {
                            this.setReadonly(true);
                            this.toFieldsEdit(['productNames'])
                        }
                        this.removeFieldValidator('sampleCode', ['remote[/secure/baseline/samplemanage/samples/action/validate-generated-samplecode]'])
                    }
                }, {
                    type: 'btnToolbar',
                    items: [{
                        type: 'button',
                        text: 'GIKAM.BUTTON.CONFIRM',
                        class: 'blue',
                        icon: 'save',
                        color: 'white',
                        hidden: _this.param.readonly && _this.param.readonly === '1',
                        onClick: function () {
                            if(_this.param.readonly === '1') {
                                Gikam.getLastModal().close();
                                return;
                            }
                            var form = Gikam.getComp('batch-sample-code-form');
                            if (form.validate()) {
                                var data = form.getData();
                                Gikam.put(limsBatch.batchSampleUrl + '/update/sample', Gikam.getJsonWrapper(null, [null, [data]])).done(function () {
                                    Gikam.getComp('batch-sample-list-grid').refresh()
                                })
                                Gikam.getLastModal().close(data);
                            }
                        }
                    }, {
                        type: 'button',
                        text: 'GIKAM.BUTTON.CANCEL',
                        icon: 'back',
                        hidden: _this.param.readonly && _this.param.readonly === '1',
                        onClick: function () {
                            Gikam.getLastModal().close();
                        }
                    }]
                }]
            }
        })
    },

    editLogisticsDetail: function (row) {
        var _this = this;
        Gikam.createLimsModal({
            title: 'GIKAM.BUTTON.EDIT',
            width : 600,
            height : 300,
            hideButton: _this.param.readonly && _this.param.readonly === '1',
            onConfirm: function () {
                var form = Gikam.getComp('sample-logistics-form');
                if (form.validate()) {
                    var data = form.getData();
                    var chooseData = {
                        id : row.id,
                        temperatureLimits : data.temperatureLimits,
                        senderName : data.senderName,
                        senderPhone : data.senderPhone,
                        courierNumber : data.courierNumber,
                        courier : data.courier,
                        senderRemark : data.senderRemark,
                    }
                    Gikam.put(limsBatch.sampleUrl + '/instant', Gikam.getJsonWrapper(null, [null, [chooseData]])).done(function () {
                        Gikam.getComp('logistics-info-grid').refreshRowById(row.id)
                    })
                    Gikam.getLastModal().close(data);
                }
            },
            m: {
                type: 'form',
                id: 'sample-logistics-form',
                columns : 2,
                titleWidth : 100,
                fields: limsBatch.getSampleLogisticsFormFields(),
                onRendered: function () {
                    this.setData(row);
                    if(_this.param.selected === '1' || _this.param.readonly === '1') {
                        this.setReadonly(true);
                    }
                }
            },
        })
    },

    getSampleToolbar : function () {
        var _this = this;
        var toolbar = [];
        if(_this.param.selected === '1') {
            toolbar = [{
                type : 'button',
                text : 'T_LIMS_SAMPLE.MODLE.BUTTON.CHOOSE_SAMPLE',
                icon : 'add',
                onClick : function() {

                    Gikam.create('modal', {
                        title: Gikam.choose.getConfig('batch-sample-choose').title,
                        url: IFM_CONTEXT + Gikam.choose.getConfig('batch-sample-choose').url+ Gikam.param({
                            status: 'inExperiment',
                            // batchId: _this.param.id,
                            originalFlg_EQ: '1'
                        }),
                        onAfterClose: function (data) {
                            console.log("onAfterChoose")
                            console.log(data)
                            if(Gikam.isEmpty(data)){
                                return
                            }
                            workspace.window.showMask();
                            Gikam.postText(limsBatch.batchSampleUrl + '/sample/choose', Gikam.getJsonWrapper({
                                batchId : _this.param.id,
                                productCode : data.productCode
                            }, [null, data.rows])).done(function () {
                                Gikam.getComp('batch-sample-list-grid').refresh();
                                Gikam.getComp('logistics-info-grid').refresh();
                            }).always(function () {
                                workspace.window.closeMask();
                            });
                        }
                    });
                }
            }]
        } else {
            toolbar = [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    Gikam.preInsert({
                        modalTitle : 'GIKAM.BUTTON.INSERT',
                        fields : limsBatch.getSampleFormFields(),
                        url : limsBatch.batchSampleUrl + '/insert/sample',
                        columns : 2,
                        titleWidth : 110,
                        width : 600,
                        height : 450,
                        disableNotEmptyValidate: true,
                        param : {
                            batchId : _this.param.id
                        }
                    }).done(function(id) {
                        Gikam.getComp('batch-sample-list-grid').refresh();
                        Gikam.getComp('logistics-info-grid').refresh();
                    });
                }
            }]
        }
        toolbar.push({
            type : 'button',
            text : 'GIKAM.BUTTON.DELETE',
            icon : 'remove-row',
            onClick : function() {
                let selections = Gikam.getComp('batch-sample-list-grid').getSelections();
                if (Gikam.isEmpty(selections)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                    return;
                }
                Gikam.confirm('GIKAM.MODAL.SUBMIT', 'GIKAM.TIP.CONFIRM_TO_DELETE', function () {
                    Gikam.del(limsBatch.batchSampleUrl + '/delete/samples', Gikam.getJsonWrapper(null, [null, selections])).done(function (req) {
                        Gikam.getComp('batch-sample-list-grid').refresh();
                        Gikam.getComp('logistics-info-grid').refresh();
                    })
                })
            }
        }) ;
        toolbar.push({
            type : 'button',
            text : 'GIKAM.BUTTON.VIEWAUDIT',
            icon : 'view-audit',
            onClick : function() {
                let activedRow = Gikam.getComp('batch-sample-list-grid').getActivedRow();
                if (Gikam.isEmpty(activedRow)) {
                    return;
                }
                // Gikam.viewAudit('T_LIMS_SAMPLE', activedRow.id);
                Gikam.viewAuditNew('T_LIMS_SAMPLE', activedRow.id);
            }
        }) ;
        return toolbar;
    },

    createPage : function() {
        Gikam.create('layout', {
            center : {
                items : [ this.getBtnToolbar(), this.getBaseInfoTab() ]
            },
            toolbarAlign: 'right',
            renderTo : Gikam.isEmpty(this.param.topPage) ? workspace.window.$dom : Gikam.getLastModal().window.$dom
        });
        return this;
    },

    init : function(param) {
        this.param = param;
        this.createPage();
    }
}
